package org.honeyweb.gsoc.dao.hibernate3;

import java.util.ArrayList;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.honeyweb.gsoc.dao.HoneypotsDao;
import org.honeyweb.gsoc.model.Honeypots;
import org.honeyweb.gsoc.model.Organizations;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class HoneypotsDaoImpl extends HibernateDaoSupport implements HoneypotsDao {

	public Honeypots getHoneypot(int honeypotId){
		if (0 == honeypotId ) {
			throw new IllegalArgumentException("honeypotsId is mandatory. Null value is forbidden.");
		}
		try {
			logger.info("get Honeypot with id: "+ honeypotId);
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			// create a new criteria
			Criteria crit = session.createCriteria(Honeypots.class);
			crit.add(Restrictions.eq("id", honeypotId));
			
			Honeypots honeypot = (Honeypots)crit.uniqueResult();
			return honeypot;
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete getOrganizations().");
			return null;
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addHoneypot(Honeypots honeypots){
		if (null == honeypots) {
			throw new IllegalArgumentException("A honeypot has to be specified");
		}	
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.save(honeypots);
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete checkLogin().");
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void delHoneypot(Honeypots honeypot){
		if (null == honeypot) {
			throw new IllegalArgumentException("A honeypot has to be specified");
		}	
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.delete(honeypot);
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete checkLogin().");
		}
	}
}
